package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.KeyValueVo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.SysFile;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.security.annotation.PreAuthorize;
import com.ruoyi.file.controller.SysFileController;
import com.ruoyi.system.domain.SysFeedback;
import com.ruoyi.system.domain.dto.AddFeedbackDto;
import com.ruoyi.system.service.ISysFeedbackService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户反馈信息Controller
 *
 * @author WJH
 * @date 2022-08-02
 */
@RestController
@RequestMapping("/system/SysFeedback")
public class SysFeedbackController extends com.ruoyi.common.core.web.controller.BaseController {
    @Autowired
    private ISysFeedbackService sysFeedbackService;
  //  @Autowired
  //  private RemoteFileService remoteFileService;
    @Autowired
    private SysFileController sysFileController;
    /**
     * 查询用户反馈信息列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:SysFeedback:list")
    //@ApiOperation(value = "获取用户反馈信息列表")
    @GetMapping("/getSysFeedbackList")
    public com.ruoyi.common.core.web.page.TableDataInfo getSysFeedbackList(SysFeedback sysFeedback) {
        startPage();
        List<SysFeedback> list = sysFeedbackService.selectSysFeedbackList(sysFeedback);
        return getDataTable(list);
    }

    /**
     * 获取用户反馈信息详细信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:SysFeedback:query")
    //@ApiOperation(value = "获取用户反馈信息详情")
    @GetMapping(value = "/getSysFeedbackById")
    public com.ruoyi.common.core.web.domain.AjaxResult getSysFeedbackById(@RequestParam("feedbackId") Long feedbackId) {
        return com.ruoyi.common.core.web.domain.AjaxResult.success(sysFeedbackService.selectSysFeedbackByFeedbackId(feedbackId));
    }

    /**
     * 新增用户反馈信息
     */
    @PreAuthorize
    @Log(title = "用户反馈信息", businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加反馈信息")
    @PostMapping(value = "/addSysFeedback")
    public com.ruoyi.common.core.web.domain.AjaxResult addSysFeedback(@RequestBody AddFeedbackDto dto) {
        SysFeedback sysFeedback = new SysFeedback();
        BeanUtils.copyProperties(dto, sysFeedback);
        String images = "";
        List<String> imageUrls = dto.getImageUrls();
        if (imageUrls != null) {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < imageUrls.size(); i++) {
                String url = imageUrls.get(i);
                if (url != null && !url.isEmpty())
                    builder.append(url);

                if (i != (imageUrls.size() - 1) )
                    builder.append(",");
            }
            images = builder.toString();
        }
        sysFeedback.setImages(images);
        sysFeedback.setCreateTime(System.currentTimeMillis());
        return toAjax(sysFeedbackService.save(sysFeedback));
    }

    /**
     * 修改用户反馈信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:SysFeedback:edit")
    @Log(title = "用户反馈信息", businessType = BusinessType.UPDATE)
    //@ApiOperation(value = "修改用户反馈信息")
    @PostMapping(value = "/editSysFeedback")
    public com.ruoyi.common.core.web.domain.AjaxResult editSysFeedback(@RequestBody SysFeedback sysFeedback) {
        return toAjax(sysFeedbackService.updateSysFeedback(sysFeedback));
    }

    /**
     * 删除用户反馈信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:SysFeedback:remove")
    @Log(title = "用户反馈信息", businessType = BusinessType.DELETE)
    @GetMapping("/removeSysFeedbacks")
    public com.ruoyi.common.core.web.domain.AjaxResult removeSysFeedbacks(@RequestParam Long[] feedbackIds) {
        return toAjax(sysFeedbackService.deleteSysFeedbackByFeedbackIds(feedbackIds));
    }

    @GetMapping("/getSysFeedbackNamesForSelect")
    //@ApiOperation(value = "获取用户反馈信息名称列表用于下拉搜索")
    public com.ruoyi.common.core.web.domain.AjaxResult getSysFeedbackNamesForSelect() {
        List<SysFeedback> list = sysFeedbackService.list();//自行加条件
        List<KeyValueVo> resultList = new ArrayList<>();
        for (SysFeedback sysFeedback : list) {
            KeyValueVo vo = new KeyValueVo();
            //vo.setKey(it.getId());
            //vo.setValue(it.getName());
            resultList.add(vo);
        }
        return com.ruoyi.common.core.web.domain.AjaxResult.success(resultList);
    }

    /**
     * 导出用户反馈信息列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:SysFeedback:export")
    @Log(title = "用户反馈信息", businessType = BusinessType.EXPORT)
    @GetMapping(value = "/exportSysFeedback", produces = "application/octet-stream")
    public String exportSysFeedback(HttpServletResponse response, SysFeedback sysFeedback) throws IOException {
        List<SysFeedback> list = sysFeedbackService.selectSysFeedbackList(sysFeedback);
        com.ruoyi.common.core.utils.poi.ExcelUtil<SysFeedback> util = new com.ruoyi.common.core.utils.poi.ExcelUtil<SysFeedback>(SysFeedback.class);
        util.exportExcel(response, list, "用户反馈信息数据");
        return "";
    }

    @ApiOperation("批量上传反馈图片")
    @PostMapping("/batchUploadImages")
    public com.ruoyi.common.core.web.domain.AjaxResult batchUploadImages(@RequestPart("images") List<MultipartFile> images) {
        R<List<SysFile>> listR = sysFileController.batchUpload(images, "feedback/images");
        if (listR.getCode() == R.FAIL) return com.ruoyi.common.core.web.domain.AjaxResult.error(listR.getMsg());
        List<String> urls = new ArrayList<>();
        if (listR.getData() != null) {
            urls = listR.getData().stream().map(SysFile::getUrl).collect(Collectors.toList());
        }
        return com.ruoyi.common.core.web.domain.AjaxResult.success(urls);
    }
}
